package com.rahman.doctor;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class AddMedicine extends Activity
{
	private Button b1, b2;	
	private TextView t1, t2, t3, t4, t5;
	private EditText e1, e2, e3, e4;	
	private CheckBox c1, c2, c3, c4;
	private RadioButton r1, r2;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addmed);
        b1 = (Button)findViewById(R.id.btnAdd);
        b2 = (Button)findViewById(R.id.btnCancel);
        
        t1 = (TextView)findViewById(R.id.textView2);
        t2 = (TextView)findViewById(R.id.textView3);
        t3 = (TextView)findViewById(R.id.textView4);
        t4 = (TextView)findViewById(R.id.textView5);
        t5 = (TextView)findViewById(R.id.textView6);
        
        e1 = (EditText)findViewById(R.id.txtName);
        e2 = (EditText)findViewById(R.id.txtType);
        e3 = (EditText)findViewById(R.id.txtDays);
        e4 = (EditText)findViewById(R.id.txtTablet);
        
        c1 = (CheckBox)findViewById(R.id.ckMor);
        c2 = (CheckBox)findViewById(R.id.ckAft);
        c3 = (CheckBox)findViewById(R.id.ckEve);
        c4 = (CheckBox)findViewById(R.id.ckNight);
        
        r1 = (RadioButton)findViewById(R.id.rbAfood);
        r2 = (RadioButton)findViewById(R.id.rbBfood);
        
        startBtnClickListener();
    }
    
    public void startBtnClickListener()
    {
    	b1.setOnClickListener(new View.OnClickListener()
    	{
    		public void onClick(View v)
    		{
    			GetUserInput();
    		}
    	});
    	
    	b2.setOnClickListener(new View.OnClickListener()
    	{
    		public void onClick(View v)
    		{
    			Clear();
    		}
    	});
    }
    
    public boolean CheckInput(TextView t, EditText e)
    {
    	if (!(e.getText().toString().equals("")))
    	{
    		t.setTextColor(Color.WHITE);
    		return true;
    	}
    	else
    	{
    		t.setTextColor(Color.RED);
    		return false;
    	}
    }
    
    public int CheckCheckBox(TextView t, CheckBox c, CheckBox cc, CheckBox ccc, CheckBox cccc)
    {
    	int ck = 0;
    	if (c.isChecked() || cc.isChecked() || ccc.isChecked() || cccc.isChecked())
    	{
    		if (c.isChecked()){ck++;} if (cc.isChecked()){ck++;} if (ccc.isChecked()){ck++;} if (cccc.isChecked()){ck++;}
    		t.setTextColor(Color.WHITE);
    		return ck;
    	}
    	else
    	{
    		t.setTextColor(Color.RED);
    		return ck;
    	}
    }
    
    public String CheckRadioButton(TextView t, RadioButton r, RadioButton rr)
    {
    	String rb = "";
    	if (r.isChecked() || rr.isChecked())
    	{
    		if(r.isChecked()){rb = r.getText().toString();} if(rr.isChecked()){rb = rr.getText().toString();}
    		t.setTextColor(Color.WHITE);
    		return rb;
    	}
    	else
    	{
    		t.setTextColor(Color.RED);
    		return rb;
    	}
    }
    
    public void GetUserInput ()
    {
    	boolean b = CheckInput(t1, e1);
    	boolean bb = CheckInput(t2, e2);
    	boolean bbb = CheckInput(t3, e3);
    	boolean bbbb = CheckInput(t4, e4);
    	
    	int i = CheckCheckBox(t5, c1, c2, c3, c4);
		String rb = CheckRadioButton(t5, r1, r2);   	
    	 	
    	if ((b) && (bb) && (bbb) && (bbbb) && (i != 0) && !(rb.equals("")))
    	{	
    		MedicineClass m = new MedicineClass(e1.getText().toString(), e2.getText().toString(), e3.getText().toString(), e4.getText().toString(), i, rb);
    		MedicineMethods.AddMedicine(m); 		
    		//Toast.makeText(getApplicationContext(), Integer.toString(mm.getMedicineList().size()), Toast.LENGTH_LONG).show();
    		
			Intent in = new Intent(AddMedicine.this, Medicine.class);
	        startActivity(in);
    	}
    }
    
    public void Clear ()
    {
    	e1.setText("");
    	e2.setText("");
    	e3.setText("");
    	e4.setText("");
    	
    	t1.setTextColor(Color.WHITE);
    	t2.setTextColor(Color.WHITE);
    	t3.setTextColor(Color.WHITE);
    	t4.setTextColor(Color.WHITE);
    	t5.setTextColor(Color.WHITE);
    	
    	c1.setChecked(false);
    	c2.setChecked(false);
    	c3.setChecked(false);
    	c4.setChecked(false);
    	
    	r1.setChecked(false);
    	r2.setChecked(false);   	
    }
}
